﻿/* 
 * This file is part of the Coffee Survey Framework - http://coffee.codeplex.com
 */
using System.Configuration;

namespace Coffee.Configuration
{

    public class ProvidersSection : ConfigurationSection
    {
        public static ProvidersSection GetConfiguration(string path)
        {
            return (ProvidersSection)ConfigurationManager.GetSection(path);
        }

        [ConfigurationProperty("providers")]
        public ConfigCollection<KeyTypeMethodElement> Providers
        {
            get { return (ConfigCollection<KeyTypeMethodElement>)base["providers"]; }
        }
    }

     public class KeyTypeMethodElement : ConfigurationElement, IUniqueIdentifier
    {
        [ConfigurationProperty("key", IsRequired = true)]
        public string Key { get { return (string)this["key"]; } set { this["key"] = value; } }
        [ConfigurationProperty("type", IsRequired = true)]
        public string Type { get { return (string)this["type"]; } set { this["type"] = value; } }
        [ConfigurationProperty("method", IsRequired = true)]
        public string Method { get { return (string)this["method"]; } set { this["method"] = value; } }
        public string Name { get { return Key; } }
    }

    public interface IUniqueIdentifier
    {
        string Name { get; }
    }

    public class ConfigCollection<T> : ConfigurationElementCollection where T : ConfigurationElement, IUniqueIdentifier, new()
    {

        protected override ConfigurationElement CreateNewElement()
        {
            return new T();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((T)element).Name;
        }

        public void Add(T element)
        {
            BaseAdd(element);
        }

        public void Clear()
        {
            BaseClear();
        }

        public void Remove(T element)
        {
            BaseRemove(element.Name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public T this[int idx]
        {
            get
            {
                return (T)BaseGet(idx);
            }
        }

        new public T this[string key]
        {
            get
            {
                return (T)BaseGet(key);
            }
        }
    }

    // Define the "font" element
    // with "name" and "size" attributes.
    public class AddKeyValue : ConfigurationElement
    {
        [ConfigurationProperty("key", IsRequired = true)]
        public string Key
        {
            get
            {
                return (string)this["key"];
            }
            set
            {
                this["key"] = value;
            }
        }

        [ConfigurationProperty("value", IsRequired = true)]
        public string Value
        {
            get
            {
                return (string)this["value"];
            }
            set
            {
                this["value"] = value;
            }
        }
    }

}
